package com.itaem.community.modules.patrol.record.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.patrol.location.entity.PatrolLocationEntity;
import com.itaem.community.modules.patrol.location.service.PatrolLocationService;
import com.itaem.community.modules.patrol.record.Util.LatLonUtil;
import com.itaem.community.modules.patrol.record.entity.PatrolRecordEntity;
import com.itaem.community.modules.patrol.record.service.PatrolRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * 巡逻记录 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "巡逻打卡记录")
@ResponseResult
@RestController
@RequestMapping("/patrolRecord")
public class PatrolRecordController {

    @Autowired
    private PatrolRecordService patrolRecordService;
    @Autowired
    private PatrolLocationService patrolLocationService;

    /**
     * @param page 当前页数是第page页
     * @param size 每页最多有size个数据
     * @return page对象
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","巡逻保安"},logical = Logical.OR)
    @ApiOperation("分页查询信息")
    @GetMapping("/{page}/{size}")
    public Page list(@PathVariable Long page, @PathVariable Long size){
        return patrolRecordService.page(new Page<PatrolRecordEntity>(page,size));
    }

    /**
     * @param id 前端传入打卡人员的id
     * @return 打卡任务是否完成
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","巡逻保安","业主"},logical = Logical.OR)
    @ApiOperation("打卡任务查询")
    @GetMapping("/{id}")
    public String info(@PathVariable Long id){
        List<PatrolRecordEntity> list = patrolRecordService.list(new QueryWrapper<PatrolRecordEntity>()
                .lambda()
                .eq(PatrolRecordEntity::getWorkerId, id));
        if (list.size() >= 3) {
            for (PatrolRecordEntity patrolRecordEntity : list) {
                if (!patrolRecordEntity.getState()) {
                    return "任务未完成";
                }
            }
        }
        return "任务完成";

    }

    /**
     * @param patrolRecordEntity 前端传入打卡人员记录的数据
     * @return 是否打卡成功
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","巡逻保安"},logical = Logical.OR)
    @ApiOperation("新增")
    @PostMapping
    public String save(@RequestBody PatrolRecordEntity patrolRecordEntity) {

        //判断是否重复地点打卡
        List<PatrolRecordEntity> list = patrolRecordService.list(new QueryWrapper<PatrolRecordEntity>().lambda().eq(PatrolRecordEntity::getWorkerId, patrolRecordEntity.getWorkerId()));
        for ( PatrolRecordEntity patrolRecordEntity1 : list){
            if(patrolRecordEntity1.getPatrolLocationId().equals(patrolRecordEntity.getPatrolLocationId())){
                return "地点重复，打卡失败！";
            }
        }

        patrolRecordEntity.setPatrolTime(LocalDateTime.now());
        //获取当前时间
        Integer id = patrolRecordEntity.getPatrolLocationId();
        PatrolLocationEntity patrolLocationEntity = patrolLocationService.getOne(new QueryWrapper<PatrolLocationEntity>().lambda().eq(PatrolLocationEntity::getCreatorId,id));
        //获取打卡地点的id

        double latitude = patrolLocationEntity.getLatitude();
        double longitude = patrolLocationEntity.getLongitude();
        double[] around = LatLonUtil.getAround(latitude,longitude,100);
        //调用工具类计算打卡地点的范围
        if (patrolRecordEntity.getLatitude()>around[0]&&patrolRecordEntity.getLatitude()<around[2]&&patrolRecordEntity.getLongitude()>around[1]&&patrolRecordEntity.getLongitude()<around[3]){
            patrolRecordEntity.setState(true);
        }else{
            patrolRecordEntity.setState(false);
        }
        return patrolRecordService.save(patrolRecordEntity) ? "打卡已成功" : "打卡失败";
    }

    @ApiOperation("更新")
    @PatchMapping
    public Boolean update(@RequestBody PatrolRecordEntity patrolRecordEntity) {
        return true;
    }

    /**
     * @param id 需要删除的数据 (逻辑删除)
     * @return 删除是否成功
     */
//    @RequiresRoles(value = {"超级管理员"},logical = Logical.OR)
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public String remove(@PathVariable Long id) {
        patrolRecordService.removeById(id);
        return patrolRecordService.removeById(id) ? "删除成功" : "没有此纪录";
    }

}

